package com.shixi.shixitraining.shixitraining.ui.tabmine.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.constant.Constants;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.http.JsonCallBack;
import com.shixi.shixitraining.shixitraining.ui.tabmine.entity.InfoBean;
import com.shixi.shixitraining.shixitraining.ui.tabmine.entity.upLoadImageBean;
import com.shixi.shixitraining.shixitraining.util.SelectPictureUtil;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.view.ChoosePictureDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class PersonInfoActivity extends BaseActivity implements View.OnClickListener {

    private EditText mUser;
    private EditText mWechat;
    private EditText mPhone;
    private EditText mEmail;
    private TextView mAddress;
    private ImageView mIcon;
    private String name;
    private String email;
    private String phone;
    private String wechat;
    private String address;
    private String country;
    private String source;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_info);
        initTitle();
    }

    private void initTitle() {

        ImageView mBackImg = (ImageView) findViewById(R.id.iv_back);
        mIcon = (ImageView) findViewById(R.id.icon_user);
        mUser = (EditText) findViewById(R.id.info_user_edt);
        mWechat = (EditText) findViewById(R.id.info_wechat_edt);
        mPhone = (EditText) findViewById(R.id.info_phone_edt);
        mEmail = (EditText) findViewById(R.id.info_email_edt);
        mAddress = (TextView) findViewById(R.id.info_address_edt);


        mBackImg.setOnClickListener(this);
        mIcon.setOnClickListener(this);
        mUser.setOnClickListener(this);
        mWechat.setOnClickListener(this);
        mPhone.setOnClickListener(this);
        mEmail.setOnClickListener(this);
        mAddress.setOnClickListener(this);
        initData();

    }

    private void initData() {

        name = mUser.getText().toString();
        email = mEmail.getText().toString();
        phone = mPhone.getText().toString();
        wechat = mWechat.getText().toString();
        address = mAddress.getText().toString();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
            case R.id.top_bar_right_tv:
                saveInfo();
                break;
            case R.id.icon_user:
                selectImage();
                break;
            case R.id.info_user_edt:
                break;
            case R.id.info_address_edt:
                selectCountry();
                break;
            case R.id.info_email_edt:
                break;
            case R.id.info_phone_edt:
                break;
            case R.id.info_wechat_edt:
                break;
        }
    }

    private void selectCountry() {
        String[] stringArray = mAddress.getResources().getStringArray(R.array.country_arrays);
        final List<String> countryList = new ArrayList<>();
        for (int i = 0; i < stringArray.length; i++) {
            countryList.add(stringArray[i]);

        }
        OptionsPickerView optionsPickerView = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                mAddress.setText(countryList.get(options1));
                country = countryList.get(options1);
                String state = countryList.get(options2);
                String city = countryList.get(options3);
            }
        }).build();

        optionsPickerView.setPicker(countryList);
        optionsPickerView.show();

    }

    private void selectImage() {
        ChoosePictureDialog dialog = new ChoosePictureDialog(this, new ChoosePictureDialog.Listener() {
            @Override
            public void choosePic() {
                SelectPictureUtil.getByAlbum(PersonInfoActivity.this);
            }

            @Override
            public void chooseCamera() {
                SelectPictureUtil.getByCamera(PersonInfoActivity.this);
            }
        });
        dialog.show();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == SelectPictureUtil.GET_BY_ALBUM) {
                SelectPictureUtil.onActivityResult(this, requestCode, resultCode, data, 0, 0);
            }
            if (requestCode == SelectPictureUtil.GET_BY_CAMERA) {
                SelectPictureUtil.onActivityResult(this, requestCode, resultCode, data, 0, 0);
            }
            if (requestCode == SelectPictureUtil.CROP) {
                if (!TextUtils.isEmpty(SelectPictureUtil.outputlsimg) && new File(SelectPictureUtil.outputlsimg).exists()) {
                    requestIcon(new File(SelectPictureUtil.outputlsimg));
                }
            }
        }

    }

    private void requestIcon(File url) {

//        Map<String, String> map = new HashMap<>();
//        map.put("image",url);
//
//        HttpUtil.getInstance().getApiInterface().upLoadImage(map).enqueue(new CallbackImple<upLoadImageBean>() {
//            @Override
//            public void onError(Call<upLoadImageBean> call, Throwable t) {
//                ToastUtil.showShortToast(PersonInfoActivity.this, t.getMessage());
//            }
//
//            @Override
//            public void onSuccess(Call<upLoadImageBean> call, upLoadImageBean response) {
//                if (response.getData() != null) {
//
//                    if (response.getStatus() == 200) {
//                        upLoadImageBean.DataBean data = response.getData();
//                        source = data.getSource();
//                        int i = Integer.parseInt(source);
//                        mIcon.setImageResource(i);
//
//                    } else {
//                        ToastUtil.showShortToast(PersonInfoActivity.this, response.getMsg());
//                    }
//
//                } else {
//                    ToastUtil.showShortToast(PersonInfoActivity.this, "数据为空");
//                }
//
//
//            }
//        });



      OkGo.<upLoadImageBean>post(Constants.UPLOADIMAGE).params("imge",url).execute(new JsonCallBack<upLoadImageBean>() {
          @Override
          public void onParseSuccess(String response, int code, String msg, upLoadImageBean data) {
              upLoadImageBean.DataBean mData = data.getData();
              String source = mData.getSource();
              String imageUrl=Constants.HOST+source;
              Log.i("www",imageUrl);
              Glide.with(PersonInfoActivity.this).load(imageUrl).into(mIcon);
          }
      });

    }

    private void saveInfo() {
        if (!TextUtils.isEmpty(mIcon.toString())) {
            return;
        } else if (!TextUtils.isEmpty(name)) {
            return;
        } else if (!TextUtils.isEmpty(country)) {
            return;
        }else if (!TextUtils.isEmpty(phone)){
            return;
        }else if (!TextUtils.isEmpty(wechat)){
            return;
        }else if (!TextUtils.isEmpty(email)){
            return;
        }
        Map<String,String> map=new HashMap<>();
        map.put("headpic",source);
        map.put("username",name);
        map.put("country",country);
        map.put("phone",phone);
        map.put("email",email);
        map.put("weixin",wechat);

        HttpUtil.getInstance().getApiInterface().info(map).enqueue(new CallbackImple<InfoBean>() {
            @Override
            public void onError(Call<InfoBean> call, Throwable t) {
                ToastUtil.showShortToast(PersonInfoActivity.this,t.getMessage());
            }

            @Override
            public void onSuccess(Call<InfoBean> call, InfoBean response) {
               if (response.getStatus()==200){

                   ToastUtil.showShortToast(PersonInfoActivity.this,response.getMsg());

               }else {
                   ToastUtil.showShortToast(PersonInfoActivity.this,"保存失败");
               }


            }
        });


    }


}
